<?php

namespace app\api\controller;

use app\api\model\OrderModel;
use think\Cache;
use think\Controller;
use app\api\service\Token;
use think\Db;

require (EXTEND_PATH.'bdxcxpay/pay/rsasigndemo.php');
class BaiduPayNofiy extends Controller
{
    //支付回调处理
    public function payNofiy()
    {
        $data = input('param.');
        $order_price = OrderModel::getOrderByOrderNum($data['tpOrderId']);
//        file_put_contents(RUNTIME_PATH.'/log/order_price.txt',$order_price['order_price']);
//        file_put_contents(RUNTIME_PATH.'/log/totalMoney.txt',$data['totalMoney']);
        if ($order_price['order_price']*100 == $data['totalMoney']){
            file_put_contents(RUNTIME_PATH.'/log/four.txt',123);
            //修改订单状态
            Db::startTrans();
            try{
              Db::name('order')->where('order_num',$data['tpOrderId'])->setField('status','2');
              Db::name('order')->where('order_num',$data['tpOrderId'])->setField('pay_time',time());
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['errno'=>'1','msg'=>'error','data'=>['isConsumed'=>1]]);
            }
            return json(['errno'=>'0','msg'=>'success','data'=>['isConsumed'=>2]]);
        }else{
            return json(['errno'=>'1','msg'=>'error','data'=>['isConsumed'=>1]]);
        }

    }
}
